package com.texaco.chapasFutbol.menu;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.camera.hud.HUD;
import org.andengine.entity.primitive.Rectangle;
import org.andengine.entity.scene.menu.MenuScene;
import org.andengine.entity.scene.menu.animator.AlphaMenuAnimator;
import org.andengine.entity.scene.menu.item.IMenuItem;
import org.andengine.entity.scene.menu.item.TextMenuItem;
import org.andengine.entity.sprite.Sprite;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.util.color.Color;

import android.opengl.GLES20;

import com.texaco.chapasFutbol.entidades.Game;
import com.texaco.chapasFutbol.resources.Resources;

public abstract class ChapasMenuScene extends MenuScene {

	protected Game game;
	private Font font;
	private HUD hud;
	private boolean postHud;

	public ChapasMenuScene(Game game, Camera camera){
		super(camera);
		this.game = game;

		ITextureRegion bgShape = this.game.getActivity().getShapesMaps().get(Game.BG_CARTEL_SHAPE);
		Sprite spBg = new Sprite(camera.getWidthRaw() / 2 - bgShape.getWidth()/2, 
				camera.getHeightRaw()/2 - bgShape.getHeight() / 2, 
				bgShape, 
				this.game.getActivity().getVertexBufferObjectManager());
		
		Rectangle bg = new Rectangle(0, 0, 
				camera.getWidthRaw(), camera.getHeightRaw(), 
				this.game.getActivity().getVertexBufferObjectManager());
		bg.setColor(Color.BLACK);
		bg.setAlpha(0.8f);
		this.attachChild(bg);
		this.attachChild(spBg);
		this.font = game.getActivity().getFontMap().get(Resources.FONT_OPTIONS);
		
		if(this.mCamera.hasHUD()){
			this.hud = this.mCamera.getHUD();
			this.mCamera.setHUD(null);
			this.postHud = true;
		}else{
			this.hud = null;
		}
	}
	
	protected TextMenuItem getMenuItem(int option, int idTexto){
		TextMenuItem tMenuItem = new TextMenuItem(option, 
				font, 
				game.getActivity().getText(idTexto),
				game.getActivity().getVertexBufferObjectManager());
		tMenuItem.setBlendFunction(GLES20.GL_SRC_ALPHA, 
				GLES20.GL_ONE_MINUS_SRC_ALPHA);
		return tMenuItem;
	}
	
	final public boolean onMenuItemClicked(final MenuScene pMenuScene, 
			final IMenuItem pMenuItem, final float pMenuItemLocalX, 
			final float pMenuItemLocalY) {
		boolean result = onOptionSelected(pMenuScene, 
			pMenuItem, pMenuItemLocalX, 
			pMenuItemLocalY);
		
		this.back();
		return result;
	}
	
	/**
	 * Indica si al salir del menú, se activará este o no.
	 * @param active
	 */
	protected void postActivateHud(boolean active){
		this.postHud = active;
	}

	public void back(){
		if(postHud){
			this.mCamera.setHUD(this.hud);
		}else{
			this.mCamera.setHUD(null);
		}
		super.back();
	}
	
	public void reset(){
		super.reset();
		postHud = false;
		this.hud = null;
	}
	
	/**
	 * Método invocado cuando se pulsa una opción del menú.
	 * @param pMenuScene
	 * @param pMenuItem
	 * @param pMenuItemLocalX
	 * @param pMenuItemLocalY
	 * @return
	 */
	abstract protected boolean onOptionSelected(MenuScene pMenuScene, IMenuItem pMenuItem,
			float pMenuItemLocalX, float pMenuItemLocalY);

	@Override
	public void buildAnimations() {
		this.setMenuAnimator(new AlphaMenuAnimator(50));
		super.buildAnimations();
	}
	
	
	
}
